package com.zch.zhongchou.controller;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zch.zhongchou.pojo.Projects;
import com.zch.zhongchou.service.AdminService;
import com.zch.zhongchou.service.ProjectService;
import com.zch.zhongchou.util.PageSize;

@Controller
@RequestMapping("/AdminController")
public class AdminController {
	@Autowired
	private AdminService as;
	@Autowired
	private ProjectService ps;
	@RequestMapping("/loginAdmin")
	public void loginAdmin(String name,String password,HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println(99999);
		Map<String, Object> map = as.login(name,password);
		boolean flag = (boolean) map.get("isLogin");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		if(flag){
			//解决弹框乱码问题
			String a = URLEncoder.encode("登录成功！", "utf-8");
			session.setAttribute("admin1", map.get("admin1"));
			out.println("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProjects'</script>");
		}else{
			String a = URLEncoder.encode((String)map.get("msg"), "utf-8");
			out.println("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='../admin/admin_login.jsp'</script>");
		}
	}
	
	@RequestMapping("/getAllProjects")
	public String getAllProjects(String pageIndexFlag,String proName, String userName, String proStatus, 
			String proType,String pageIndex,HttpServletRequest request){
		if(proStatus == null ||proStatus ==""){
			proStatus = "-1";
		}
		if(proType == null ||proType ==""){
			proType = "-1";
		}
		if(pageIndex == null ||pageIndex ==""){
			pageIndex = "1";
		}
		int proStatus2 = Integer.parseInt(proStatus);
		int proType2 = Integer.parseInt(proType);
		int pageIndex2 = Integer.parseInt(pageIndex);
		//获得项目信息
		List<Projects> list = ps.getAllProjectsByPage(proName, userName, proStatus2, proType2, pageIndex2);
		//获得总数据量
		int totalCounts = ps.getTotalCounts(proName, userName, proStatus2, proType2);
		//获得总页码
		int totalPages = ps.getTotalPages(proName, userName, proStatus2, proType2);
		//设置序号
		int i = (pageIndex2 - 1)*PageSize.PAGE_SIZE;
		request.setAttribute("i", i);
		request.setAttribute("proName", proName);
		request.setAttribute("userName", userName);
		request.setAttribute("proStatus", proStatus);
		request.setAttribute("proType", proType);
		request.setAttribute("pageIndexBegin", pageIndex);
		if(pageIndexFlag==null){
			pageIndexFlag="1";
		}
		int pageIndexFlag2=Integer.parseInt(pageIndexFlag);
		System.out.println("pageIndex:"+pageIndex);
		System.out.println("pageIndexFlag:"+pageIndexFlag);
		if(pageIndex2==pageIndexFlag2+4){
			pageIndexFlag2++;
		}
		if(pageIndex2==pageIndexFlag2&&pageIndex2!=1){
			pageIndexFlag2--;
		}
		request.setAttribute("pageIndex",pageIndex);
		request.setAttribute("pageIndexFlag",pageIndexFlag2);
		request.setAttribute("list", list);
		request.setAttribute("totalCounts", totalCounts);
		request.setAttribute("totalPages", totalPages);
		return "admin/products";
	}
	
	@RequestMapping("outAdmin")
	public String doOut(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.removeAttribute("admin1");
		return "redirect:toIndex";
	}
	@RequestMapping("toIndex")
	public String toIndex(){
		return "/index";
	}
}
